<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

/**
 * Class Address
 * @package App\Model
 * @User wuqin
 * @Date 2018/3/14 0014
 * @Time 12:53
 */
class Address extends Common
{
    /**
     * @var bool
     */
    public $timestamps = false;
    /**
     * @var string
     */
    protected $primaryKey = 'id';
    /**
     * @var string
     */
    protected $table = 'address';

    /**
     * 方法 : get_all_address
     * @User wuqin
     * @DateTime 2018-03-14 12:53
     * @return mixed
     */
    public function get_all_address()
    {
        $key_index = 'address:*';
        if (empty($keys = $this->keys_redis($key_index))) {
            $data = $this->get(array(), '*', 0, 0, ['parent', 'asc']);
            if (empty($data))
                $this->set_empty_array_redis('address:0');
            $this->set_array($data);
        }
        $keys = $this->get_all_keys($key_index);
        $data = $this->mget_array_redis($keys);
//        $data = $this->sort_address($data);
        return $data;
    }

    /**
     * 方法 : set_array
     * @User wuqin
     * @DateTime 2018-03-29 20:30
     * @param $data
     * @return bool
     */
    public function set_array($data)
    {
        if (empty($data))
            return false;
        foreach ($data as $key => $value) {
            $this->set_array_redis('address:' . $value['id'] . ':rank:' . $value['rank'] . ':parent:' . $value['parent'], $value);
        }
    }

    /**
     * 方法 : get_all_keys
     * @User wuqin
     * @DateTime 2018-03-29 20:30
     * @return mixed
     */
    public function get_all_keys()
    {
        $key_index = 'address_all_key';
        if (empty($keys = $this->exists_redis($key_index))) {
            $this->set_array_redis('address_all_key', $this->keys_redis("address:*"));
        }
        $data = $this->mget_array_redis('address_all_key');
        return reset($data);
    }

    /**
     * 方法 : get_address
     * @User wuqin
     * @DateTime 2018-03-29 20:30
     * @param int $id
     * @return array|mixed
     */
    public function get_address($id = 0)
    {
        $key_index = 'address:*:rank:*:parent:*';
        if ($id !== 0) {
            $where[0] = array('where', 'id', '=', $id);
            $key_index = 'address:'.$id.':rank:*:parent:*';
        }
        if (empty($keys = $this->keys_redis($key_index))) {
            $data = $this->get($where, '*', 0, 0, ['id', 'asc']);
            if (empty($data))
                $this->set_empty_array_redis('address:' . $id . ':rank:-1:parent:0');
            $this->set_array($data);
        }
        $keys = $this->get_all_keys($key_index);
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    /**
     * 方法 : get_address_by_key
     * @User wuqin
     * @DateTime 2018-03-30 14:31
     * @param $data
     * @return bool|mixed
     */
    public function get_address_by_key($var)
    {
        if (count($var) != 3)
            return false;
        $key_index = '';
        foreach ($var as $key => $value) {
            if (!in_array($key, ["address", "rank", "parent"])) {
                return false;
            }
            if ($key === "parent") {
                $key_index .= $key . ':' . $value;
                continue;
            }
            $key_index .= $key . ':' . $value . ":";
        }
        if (empty($keys = $this->exists_redis($key_index))) {
            $where = array(
                array('where', 'id', '=', $var['address']),
                array('where', 'rank', '=', $var['rank']),
                array('where', 'parent', '=', $var['parent']),
            );
            $data = $this->get($where, '*', 0, 0, ['id', 'asc']);
            if (empty($data))
                $this->set_empty_array_redis('address:' . $var['address'] . ':rank:' . $var['rank'] . ':parent:' . $var['parent']);
            $this->set_array($data);
        }
        $data = $this->mget_array_redis($key_index);
        if (count($data) === 1)
            return reset($data);
        else
            return false;
    }
    /**
     * 方法 : get_provinces
     * @User wuqin
     * @DateTime 2018-03-29 20:30
     * @param int $id
     * @return array|mixed
     */
    public function get_provinces($id = 0)
    {
        $key_index = 'address:*:rank:0:parent:*';
        $where[0] = array('where', 'rank', '=', 0);
        if ($id !== 0) {
            $where[1] = array('where', 'id', '=', $id);
            $key_index = 'address:'.$id.':rank:0:parent:*';
        }
        if (empty($keys = $this->keys_redis($key_index))) {
            $data = $this->get($where, '*', 0, 0, ['id', 'asc']);
            if (empty($data))
                $this->set_empty_array_redis('address:'.$id.':rank:0:parent:-1');
            $this->set_array($data);
        }
        $keys = $this->keys_redis($key_index);
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    /**
     * 方法 : get_citys
     * @User wuqin
     * @DateTime 2018-03-29 20:30
     * @param int $parent
     * @return array|mixed
     */
    public function get_citys($parent = 0)
    {
        $key_index = 'address:*:rank:1:parent:*';
        $where[0] = array('where', 'rank', '=', 1);
        if ($parent !== 0) {
            $where[1] = array('where', 'parent', '=', $parent);
            $key_index = 'address:*:rank:1:parent:'.$parent;
        }
        if (empty($keys = $this->keys_redis($key_index))) {
            $data = $this->get($where, '*', 0, 0, ['parent', 'asc']);
            if (empty($data))
                $this->set_empty_array_redis('address:0:rank:1:parent:' . $parent);
            $this->set_array($data);
        }
        $keys = $this->keys_redis($key_index);
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    /**
     * 方法 : get_county
     * @User wuqin
     * @DateTime 2018-03-29 20:30
     * @param int $parent
     * @return array|mixed
     */
    public function get_county($parent = 0)
    {
        $key_index = 'address:*:rank:2:parent:*';
        $where[0] = array('where', 'rank', '=', 2);
        if ($parent !== 0) {
            $where[1] = array('where', 'parent', '=', $parent);
            $key_index = 'address:*:rank:2:parent:'.$parent;
        }
        if (empty($keys = $this->keys_redis($key_index))) {
            $data = $this->get($where, '*', 0, 0, ['parent', 'asc']);
            if (empty($data))
                $this->set_empty_array_redis('address:0:rank:2:parent:' . $parent);
            $this->set_array($data);
        }
        $keys = $this->keys_redis($key_index);
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    /**
     * 方法 : test
     * @User wuqin
     * @DateTime 2018-03-14 12:53
     * @param int $parent
     */
    public function test($parent = 0)
    {
//        $key_index = 'address:*';
//        $keys = $this->keys_redis($key_index);
//        $this->set_array_redis('address_all_key',$keys);
        return $this->mget_array_redis('address_all_key');
//         $data;
    }

    /**
     * 方法 : set_empty
     * @User wuqin
     * @DateTime 2018-03-29 20:30
     * @param string $id
     * @param string $rank
     * @param string $parent
     */
    public function set_empty($id = '0', $rank = '-1', $parent = '0')
    {
        $this->set_empty_array_redis('address:' . $id . ':rank:'.$rank.':parent:' . $parent);
    }

    /**
     * 方法 : turn_address
     * @User wuqin
     * @DateTime 2018-03-29 20:30
     * @param $data
     * @return array|bool
     */
    public function turn_address($data)
    {
        if (empty($data))
            return false;

        //整理数据
        $arm = array();
        $parent = 0;
        $children_data = array();
        $result = array();
        foreach ($data as $key => $value) {
            if ($value['parent'] == 0) {
                $arm[$value['id']] = $key;
                $result[$value['id']] = $value;
                continue;
            }
            if ($parent != $value['parent']) {
                if ($parent == 0) {
                    $parent = $value['parent'];
                } else {
                    $result[$parent]['data'] = $children_data;
                    $children_data = array();
                    $parent = $value['parent'];
                }
            }

            if ($value['parent'] == $parent) {
                $children_data[$value['id']] = $value;
            }

        }
        $result[$parent]['data'] = $children_data;
        return $result;
    }

    /**
     * 方法 : sort_address
     * @User wuqin
     * @DateTime 2018-03-29 20:30
     * @param $data
     * @return array
     */
    public function sort_address($data)
    {
        if (empty($data))
            return array();
        //多维排序
        $parent = array();
        foreach ($data as $key => &$value) {
            $value = json_decode($value, true, 512, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
            if (empty($value)) {
                unset($data[$key]);
                continue;
            }
            $parent[] = $value['parent'];
        }
        unset($value);
        if (!empty($data))
            array_multisort($parent, SORT_ASC, $data);
        return $data;
    }
}
